package cn.zn.modules.appoin.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.zn.modules.appoin.dao.AtSpecialMapper;
import cn.zn.modules.appoin.entity.AtSpecial;
import cn.zn.modules.appoin.entity.AtSpecialWorkTime;
import cn.zn.modules.appoin.service.AtSpecialService;
import cn.zn.modules.appoin.service.AtSpecialWorkTimeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class AtSpecialServiceImpl extends ServiceImpl<AtSpecialMapper, AtSpecial> implements AtSpecialService {

    @Autowired
    private AtSpecialWorkTimeService atSpecialWorkTimeService;

    @Override
    public void saveSpecial(AtSpecial atSpecial) {
        if (StrUtil.isNotBlank(atSpecial.getId())) {
            super.updateById(atSpecial);
            // 删除工作时间
            atSpecialWorkTimeService.remove(new QueryWrapper<AtSpecialWorkTime>().eq("SPECIAL_ID_", atSpecial.getId()));
        } else {
            super.save(atSpecial);
        }
        // 保存工作时间
        if (atSpecial.getSpecialWorkTimes() != null) {
            atSpecial.getSpecialWorkTimes().forEach(o -> {
                o.setSpecialId(atSpecial.getId());
                atSpecialWorkTimeService.save(o);
            });
        }
    }

    @Override
    public void updateSpecial(AtSpecial atSpecial) {
        super.updateById(atSpecial);
        // 删除工作时间
        atSpecialWorkTimeService.remove(new QueryWrapper<AtSpecialWorkTime>().eq("SPECIAL_ID_", atSpecial.getId()));
        // 保存工作时间
        if (atSpecial.getSpecialWorkTimes() != null) {
            atSpecial.getSpecialWorkTimes().forEach(o -> {
                o.setSpecialId(atSpecial.getId());
                atSpecialWorkTimeService.save(o);
            });
        }
    }

    @Override
    public AtSpecial queryByUserAndDate(String specialDate, String userCode) {
        AtSpecial atSpecial = this.baseMapper.queryByUserAndDate(specialDate, userCode);
        if (atSpecial != null) {
            List<AtSpecialWorkTime> workTimes = atSpecialWorkTimeService.list(new QueryWrapper<AtSpecialWorkTime>()
                    .eq("SPECIAL_ID_", atSpecial.getId())
                    .orderByAsc("START_TIME_"));
            atSpecial.setSpecialWorkTimes(workTimes);
        }
        return atSpecial;
    }

    @Override
    public List<AtSpecial> queryByUserAndInterval(String startDate, String endDate, String ruleId) {
        return super.list(new QueryWrapper<AtSpecial>()
                .eq("RULE_ID_", ruleId)
                .between("SPECIAL_DATE_", startDate, endDate));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        atSpecialWorkTimeService.remove(new QueryWrapper<AtSpecialWorkTime>().in("SPECIAL_ID_", idList));
        super.removeByIds(idList);
        return true;
    }
}
